package com.qicai.tools.generator.executor;

import com.qicai.tools.generator.util.FieldUtils;
import com.qicai.tools.generator.util.MyFile;
import com.qicai.tools.generator.util.TableInfo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mysql数据库映射生成JAVA代码
 *
 * @author liuxl
 */
public class MysqlGenerator {

    private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/hehe?useUnicode=true&characterEncoding=UTF-8&noDatetimeStringSync=true";
    //    private static final String DB_URL      = "jdbc:mysql://localhost:3306/qicai_shop";
    private static final String DB_USERL = "root";
    private static final String DB_PWD = "root";

    // 顶层包名
    private static final String TOP_LEVEL = "com.xb.loan.app";
    // 模块（加上顶层包名，最后生成如：com.oneclass.service）
    private static final String MODULE_NAME = "service";

    /**
     * 存放生成的JAVA文件的目录
     */
    private static final String SRC_FOLDER = "dbJavaTools/test/src/main/java/";
    /**
     * 存放生成的jsp文件的目录
     */
    private static final String WEB_FOLDER = "dbJavaTools/test/src/main/webapp/WEB-INF";

    /**
     * 获得表信息
     *
     * @param tableNames 表集合
     * @return
     * @throws SQLException
     */
    private static List<TableInfo> getTableInfo(Map<String, String> tableNames) throws SQLException {
        List<TableInfo> tables = new ArrayList<TableInfo>();
        for (String key : tableNames.keySet()) {
            TableInfo ti = new TableInfo(key, tableNames.get(key), FieldUtils.getMysqlFieldInfo(key, DB_URL, DB_USERL,
                    DB_PWD));
            tables.add(ti);
        }
        return tables;
    }

    public static void main(String[] args) {
        Gen tool = new Gen();
        try {
            // 用于建立目录和写文件
            MyFile myfile = new MyFile(SRC_FOLDER, WEB_FOLDER);

            // 将需要添加的表存入map中，key为表名，value为生成的实体类的注释
            Map<String, String> map = new HashMap<String, String>();
//            map.put("carts", "购物车管理");
//            map.put("orders", "订单管理");7
//            map.put("express", "快递公司维护");
//            map.put("express_template", "快递模板");
            map.put("t_loan_platform", "贷款平台");
            map.put("t_loan_platform_info", "贷款平台申请流程、条件、资料信息配置");
            map.put("t_loan_platform_type", "贷款平台特征等信息配置");


            //准备表对象
            List<TableInfo> tables = getTableInfo(map);
            tool.gen(tables, myfile, TOP_LEVEL, MODULE_NAME);
            System.out.println("=======共计生成表：========" + tables.size() + "张");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
